//
//  WJDataManager.m
//  ChatOnLine
//
//  Created by dllo on 16/8/11.
//  Copyright © 2016年 WWT. All rights reserved.
//

#import "WJDataManager.h"
#import <FMDB.h>

@interface WJDataManager ()

@property (nonatomic, strong) FMDatabase *fmdb;

@property (nonatomic, assign) CGFloat totleDis;

@property (nonatomic, assign) CGFloat totleTime;

@end

@implementation WJDataManager

+ (instancetype)shareFMDB
{
    static WJDataManager *fmdb = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        fmdb = [[WJDataManager alloc]init];
    });
    return fmdb;
}

// 这是创建文件
- (void)openDB
{
    NSString *filePatch = [WJDOCUMENT stringByAppendingPathComponent:@"ChatOnLine.sqlite"];
    self.fmdb = [[FMDatabase alloc] initWithPath:filePatch];
    BOOL result = [self.fmdb open];
    if (result) {
        NSLog(@"=->OPEN DB Successd");
    } else {
        NSLog(@"=->OPEN DB Failure");
    }
}


// 这是创建文件里面的表格
- (void)createSportTable
{
    NSString *sqlStr  = @"create table if not exists sport (time text primary key,distance text,useTime text) ";
    
    [self executeSqlWithStr:sqlStr showLog:@"CREATE Sport TABLE"];
}

//增  什么时候跑步 数据库存储格式为 2016-08-11 20:38:30  一共用时:秒  总距离:m
- (void)insertWithTime:(NSString *)time Diatance:(NSString *)distance UseTime:(NSString *)useTime
{
    NSString *sqlStr =[NSString stringWithFormat:@"insert into sport (time,distance,useTime) values ('%@','%@','%@')",time,distance,useTime];
    
    [self executeSqlWithStr:sqlStr showLog:@"INSERT"];
}

//删
- (void)deleteWithUseTime:(NSString *)useTime
{
    NSString *sqlStr = [NSString stringWithFormat:@"delete from sport where time = '%@' ",useTime];
    
    [self executeSqlWithStr:sqlStr showLog:@"DELETE FOLLOW"];
}


//查
- (NSMutableArray *)selectAll
{
    NSMutableArray *array = [NSMutableArray array];
    
    NSString *sql = [NSString stringWithFormat:@"select *from sport"];
    //执行查询语句
    FMResultSet *set = [self.fmdb executeQuery:sql];
    //FMResultSet 是用来遍历表中符合要求的每一行
    while ([set next]) {
        
        NSString *time = [set objectForColumnName:@"time"];
        NSString *distance = [set objectForColumnName:@"distance"];
        NSString *useTime = [set objectForColumnName:@"useTime"];
        NSArray *arr = @[time,distance,useTime];
        
        [array addObject:arr];
    }
    
    return array;
}

- (NSArray *)totleDistance
{
    NSMutableArray *array = [self selectAll];
    
    _totleDis = 0.00;
    _totleTime = 0.00;
    
    if (array.count != 0) {
        
        for (NSArray *arr in array) {
            
            NSString *distance = arr[1];
            _totleDis = _totleDis + distance.floatValue;
                        
            NSString *time = arr[2];
            _totleTime = _totleTime + time.floatValue;
        }
        
        NSString *totleDistance = [NSString stringWithFormat:@"%.2f",_totleDis];
        NSString *totleTime = [NSString stringWithFormat:@"%.2f",_totleTime];
        NSString *totleCount = [NSString stringWithFormat:@"%d",array.count];
        
        NSLog(@"%f",_totleDis);
        
        return @[totleDistance,totleTime,totleCount];
    }else{
        return @[@"0.00",@"0.0",@"0"];
    }
}





#pragma mark - 封装方法
- (void)executeSqlWithStr:(NSString *)sql showLog:(NSString *)message
{
    //执行语句
    BOOL result = [self.fmdb executeUpdate:sql];
    NSString *string = @"";
    if (result) {
        string = [NSString stringWithFormat:@"%@ Succeed", message];
    } else {
        string = [NSString stringWithFormat:@"%@ Failure", message];
    }
    NSLog(@"=->%@", string);
}

@end
